import cmescourses.server.*;

import javax.naming.Context;
import javax.naming.InitialContext;

public class ClientShoppingCart {

  public static void main(String[] args) throws Exception {    

    // Get a naming context
    Context jndiContext = new InitialContext();
    System.out.println("Got context");
    
    // Get a reference to the ShoppingCartEJB Home interface
    ShoppingCartHome scHome = (ShoppingCartHome)jndiContext.lookup("ShoppingCartEJB"); // cle trouvée dans le ejb-jar.xml du serveur
    System.out.println("Got ShoppingCartEJB Bean reference");

    // Create a ShoppingCartEJB object from the Home interface
    ShoppingCart sc = scHome.create();
    System.out.println("ShoppingCartEJB Created");

    // Method invocation test
    System.out.println("Item count: " + sc.getItemCount());

    // Creer 3 instances de la classe ShoppingCartItem
    ShoppingCartItem item1 = new ShoppingCartItem(5, "item 1 desc", "item 1 serial");
    ShoppingCartItem item2 = new ShoppingCartItem(6, "item 2 desc", "item 2 serial");
    ShoppingCartItem item3 = new ShoppingCartItem(7, "item 3 desc", "item 3 serial");
    System.out.println("3 items created");

    // Placer les items dedans grace à l'interface remote qui fournir addItem()
    sc.addItem(item1);
    sc.addItem(item2);
    sc.addItem(item3);
    System.out.println("3 items added to shopping cart");

    // Verification du nb d'items
    System.out.println("Item count: " + sc.getItemCount());

    // Extraction du 2e element du bean et affichage de son descriptif
    ShoppingCartItem itemReturned = sc.getItem(2);
    System.out.println("Extraction 2e item et affichage: " + itemReturned);

    // Suppression du 1er element
    sc.removeItem(item1);

    // Nombre d'articles restant
    System.out.println("Item count: " + sc.getItemCount());

  }

}
